# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QVBoxLayout
from qfluentwidgets import SingleDirectionScrollArea, SimpleCardWidget, setFont, HorizontalSeparator

from components import BookCard, GridLayout, ElidedBodyLabel


class TermItemWidget(SimpleCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.titleLabel = ElidedBodyLabel(self)
        self.gridLayout = GridLayout()

        setFont(self.titleLabel, 20)
        self.titleLabel.setFixedHeight(50)
        self.setMinimumHeight(390)
        self.gridLayout.setItemMinimumWidth(200)

        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
        self.verticalLayout.addWidget(HorizontalSeparator(self), 1)
        self.verticalLayout.addLayout(self.gridLayout)

    def setTitile(self, title: str):
        self.titleLabel.setText(title)

    def addBookCard(self, bookData: dict):
        bookCard = BookCard(self)
        bookCard.setBookData(bookData)
        self.gridLayout.addWidget(bookCard)

    def clear(self):
        self.gridLayout.takeAllWidgets()


class ItemScrollArea(SingleDirectionScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.view = QWidget(self)
        self.gridLayout = GridLayout(self.view)

        self.gridLayout.setItemMinimumWidth(200)
        self.gridLayout.setContentsMargins(0, 0, 15, 0)

        self.setWidget(self.view)
        self.setWidgetResizable(True)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        self.enableTransparentBackground()

    def addBookCard(self, bookData: dict):
        bookCard = BookCard(self)
        bookCard.setBookData(bookData)
        self.gridLayout.addWidget(bookCard)

    def clear(self):
        self.gridLayout.takeAllWidgets()


class TermItemScrollArea(SingleDirectionScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.view = QWidget(self)
        self.verticalLayout = QVBoxLayout(self.view)
        self.termsMap = {}

        self.setWidget(self.view)
        self.setWidgetResizable(True)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        self.enableTransparentBackground()

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setSpacing(20)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)

    def addTermItemWidget(self, term: Union[str, int]):
        if term in self.termsMap:
            return self.termsMap[term]
        termItemWidget = TermItemWidget(self)
        termItemWidget.setTitile(f'第 {term} 学期')
        self.verticalLayout.addWidget(termItemWidget)
        self.termsMap[term] = termItemWidget
        return termItemWidget

    def addBookCard(self, bookData: dict):
        term = bookData['term']
        termItemWidget = self.addTermItemWidget(term)
        termItemWidget.addBookCard(bookData)

    def clear(self):
        for termItemWidget in self.termsMap.values():
            self.verticalLayout.removeWidget(termItemWidget)
            termItemWidget.deleteLater()
        self.termsMap.clear()
